package yunwo.cn.yzstation.view.fregment.carsdetail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;

import java.util.List;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.adapter.GoodsInfoadatper;
import yunwo.cn.yzstation.bean.CarListBean;
import yunwo.cn.yzstation.bean.GoodsinfoitemBean;
import yunwo.cn.yzstation.presenter.GoodsInfoFramgnetPresenter;
import yunwo.cn.yzstation.presenter.IGoodsinfoView;
import yunwo.cn.yzstation.utils.NetWorkUtils;
import yunwo.cn.yzstation.view.activity.CarsDetailsActivity;

/**
 * Created by lao on 2017/10/13.
 */

public class GoodsInfoFregment extends Fragment implements IGoodsinfoView{

    private View mRootView;
    private CarsDetailsActivity mActivity;
    private XRefreshView mXrefreshview;
    private ListView mMyListview;
    private GoodsInfoFramgnetPresenter mGoodsInfoFramgnetPresenter;
    private LinearLayout mZhuangtai;
    private TextView mZhuangtaiText;
    private ImageView mZhuangtaiIMG;
    private CarListBean.DataBean.ListBean mData;
    private GoodsInfoadatper mGoodsInfoadatper;
    GoodsinfoitemBean.DataBean mDataBean;
    int cout = 1;
    private List<GoodsinfoitemBean.DataBean.ListBean> mList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_goodsinfo, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = (CarsDetailsActivity) getActivity();
        mData = mActivity.getData();
        mGoodsInfoFramgnetPresenter = new GoodsInfoFramgnetPresenter(mActivity,this,this);
        initView();
        initData();
    }
    private void initData() {
        if (!NetWorkUtils.isNetworkConnected(getContext())) {
            if (mZhuangtai != null) {
                mZhuangtai.setVisibility(View.VISIBLE);
                mZhuangtaiIMG.setImageResource(R.drawable.icon_no_network);
                mZhuangtaiText.setText("网络出错,请检查您的网络~");
                mMyListview.setVisibility(View.GONE);
            }
        }else {
            if (mZhuangtai != null) {
                mZhuangtai.setVisibility(View.GONE);
            }
        }

        mGoodsInfoFramgnetPresenter.getData(mData.getDepartId(),"",1,5);

    }
    private void initView() {
        mXrefreshview = (XRefreshView) mRootView.findViewById(R.id.xrefreshview);
        mMyListview = (ListView) mRootView.findViewById(R.id.myListview);
        mZhuangtai = (LinearLayout) mRootView.findViewById(R.id.zhuangtai);
        mZhuangtaiText = (TextView) mRootView.findViewById(R.id.zhuangtaiText);
        mZhuangtaiIMG = (ImageView) mRootView.findViewById(R.id.zhuangtaiIMG);
        mGoodsInfoadatper = new GoodsInfoadatper(getContext());
        mMyListview.setAdapter(mGoodsInfoadatper);
        mXrefreshview.setPullLoadEnable(true);
        mXrefreshview.setPullRefreshEnable(true);
        mXrefreshview.setAutoLoadMore(false);
        mXrefreshview.setScrollBackDuration(300);
        mXrefreshview.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {

                mGoodsInfoFramgnetPresenter.getData(mData.getDepartId(),"1",1,5);

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                if (mDataBean!=null && mDataBean.isHasNextPage()) {
                    cout += 1;
                    mGoodsInfoFramgnetPresenter.getData(mData.getDepartId(),"2",1,5);
                    mXrefreshview.stopLoadMore();
                }else {
                    Toast.makeText(getContext(), "已加载全部数据", Toast.LENGTH_SHORT).show();
                    mXrefreshview.stopLoadMore(true);
                }

            }
        });
    }

    @Override
    public void getDatafailure() {
        if (mZhuangtai != null) {
            mZhuangtai.setVisibility(View.VISIBLE);
            mZhuangtaiIMG.setImageResource(R.drawable.icon_no_network);
            mZhuangtaiText.setText("网络开小差了");
            mMyListview.setVisibility(View.GONE);
            mXrefreshview.setVisibility(View.GONE);
        }
    }

    @Override
    public void getDataSueecss(String type, GoodsinfoitemBean.DataBean dataBean) {
        if (dataBean.getList().size() == 0) {

            if (mZhuangtai != null) {
                mZhuangtai.setVisibility(View.VISIBLE);
            }
        }else {
            if (mZhuangtai != null) {
                mZhuangtai.setVisibility(View.GONE);
            }
        }
        this.mDataBean = dataBean;
        switch (type) {
            case "":
                mList = dataBean.getList();
                mGoodsInfoadatper.refreshDataAndNotifyDataSetChanged(dataBean.getList());
                break;
            case "1":
                if (mXrefreshview != null) {
                    mXrefreshview.stopRefresh();
                    cout = 1;
                    if (dataBean.getList().size() == 0) {
                        Toast.makeText(getContext(),"暂无数据",Toast.LENGTH_SHORT).show();

                    }else {
                        Toast.makeText(getContext(), "刷新成功", Toast.LENGTH_SHORT).show();
                    }
                    mList = dataBean.getList();
                    mGoodsInfoadatper.refreshDataAndNotifyDataSetChanged(dataBean.getList());
                }
                break;
            case "2":
                if (mXrefreshview != null) {
                    mXrefreshview.stopLoadMore();
                    Toast.makeText(getContext(), "加载成功", Toast.LENGTH_SHORT).show();
                    mList.addAll(dataBean.getList());
                    mGoodsInfoadatper.addDataAndNotifyDataSetChanged(dataBean.getList());
                }
                break;

        }
    }
}
